package com.dzl.integral;

import com.alibaba.fastjson.JSONObject;
import com.dzl.admin.pojo.vo.LoginSysAdminVo;
import com.dzl.base.controller.BaseController;
import com.dzl.enterprise.pojo.WelfareEnterprise;
import com.dzl.enterprise.service.WelfareEnterpriseService;
import com.dzl.integral.pojo.WelfareIntegralGrant;
import com.dzl.integral.pojo.dto.WelfareIntegralGrantDto;
import com.dzl.integral.pojo.query.WelfareIntegralGrantDetailQuery;
import com.dzl.integral.pojo.query.WelfareIntegralGrantQuery;
import com.dzl.integral.pojo.vo.WelfareIntegralGrantDetailVo;
import com.dzl.integral.pojo.vo.WelfareIntegralGrantVo;
import com.dzl.integral.service.WelfareIntegralGrantDetailService;
import com.dzl.integral.service.WelfareIntegralGrantService;
import com.dzl.util.DateUtil;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author caobucao
 * @description: saas服务端积分相关(发放积分，积分管理)
 * @since 12:43 2021/09/06
 */
@RestController
@RequestMapping("welfareIntegralGrant")
public class WelfareIntegralGrantController extends BaseController {

    @Autowired
    private WelfareIntegralGrantService service;
    @Autowired
    private WelfareEnterpriseService welfareEnterpriseService;
    @Autowired
    private WelfareIntegralGrantDetailService welfareIntegralGrantDetailService;

    /**
     * 获取企业积分信息
     * @date 2021/9/7 10:16
     * @author caobucao
     * @return com.dzl.util.JsonResult<com.dzl.admin.pojo.vo.LoginSysAdminVo>
     */
    @GetMapping("/getIntegralInfo")
    public JsonResult<WelfareEnterprise> getIntegralInfo() {
        Integer enterpriseId = getAdminInfo().getEnterpriseId();
        WelfareEnterprise welfareEnterprise = welfareEnterpriseService.selectByPrimaryKey(enterpriseId);
        return JsonResult.success(welfareEnterprise);
    }

    /**
     * 分页查询分发积分数据
     * @date 2021/9/7 11:02
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.integral.pojo.vo.WelfareIntegralGrantVo>
     */
    @GetMapping("/getIntegralGrantPage")
    public PageUtils<WelfareIntegralGrantVo> getIntegralGrantPage(WelfareIntegralGrantQuery query) {
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        List<WelfareIntegralGrantVo> list = service.getIntegralGrantPage(query);
        PageInfo<WelfareIntegralGrantVo> pageInfo = new PageInfo<>(list);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 分页查询积分分发详情
     * @date 2021/9/7 14:11
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.integral.pojo.vo.WelfareIntegralGrantDetailVo>
     */
    @GetMapping("/getIntegralGrantDetail")
    public PageUtils<WelfareIntegralGrantDetailVo> getIntegralGrantDetail(WelfareIntegralGrantDetailQuery query) {
        List<WelfareIntegralGrantDetailVo> list = welfareIntegralGrantDetailService.getIntegralGrantDetail(query);
        PageInfo<WelfareIntegralGrantDetailVo> pageInfo = new PageInfo<>(list);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 终止积分发放
     * @date 2021/9/8 17:11
     * @author caobucao
     * @param params 发放id
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/updateGrantToStop")
    public JsonResult<BigDecimal> updateGrantToStop(@RequestBody JSONObject params) {
        Integer grantId = params.getInteger("grantId");
        if(EmptyUtils.isEmpty(grantId)) {
            return JsonResult.paramErrWithMsg("请选择要终止发放的数据！");
        }
        WelfareIntegralGrant welfareIntegralGrant = new WelfareIntegralGrant();
        welfareIntegralGrant.setId(grantId);
        welfareIntegralGrant.setGrantState(3); //分发状态：1分发中，2已到账，3已终止
        welfareIntegralGrant.setUpdater(getAdminInfo().getId());
        welfareIntegralGrant.setEnterpriseId(getAdminInfo().getEnterpriseId());
        return JsonResult.success(service.updateGrantToStop(welfareIntegralGrant));
    }

    /**
     * 积分分发
     * @date 2021/9/7 15:41
     * @author caobucao
     * @param dto 数据传输类
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/saveIntegralGrant")
    public JsonResult<Integer> saveIntegralGrant(@RequestBody WelfareIntegralGrantDto dto) {
        if(EmptyUtils.isEmpty(dto.getName())) {
            return JsonResult.paramErrWithMsg("请输入名称！");
        }
        if(EmptyUtils.isEmpty(dto.getIntegral())) {
            return JsonResult.paramErrWithMsg("请输入面值！");
        }
        if(EmptyUtils.isEmpty(dto.getUserIdList())) {
            return JsonResult.paramErrWithMsg("请选择要分发的成员！");
        }
        if(EmptyUtils.isEmpty(dto.getAccountDate())) {
            //到账时间等于发放时间+2个小时
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.HOUR, 2);
            Date accountDateTime = cal.getTime();
            dto.setAccountDateTime(accountDateTime);
        } else {
            dto.setAccountDateTime(DateUtil.parseDateTime(dto.getAccountDate()));
        }
        if(EmptyUtils.isEmpty(dto.getExpireDate())) {
            return JsonResult.paramErrWithMsg("请选择积分有效期！");
        }
        LoginSysAdminVo adminVo = getAdminInfo();
        dto.setExpireDateTime(DateUtil.parseDateTime(dto.getExpireDate()));
        if(dto.getAccountDateTime().getTime() > dto.getExpireDateTime().getTime()) {
            return JsonResult.paramErrWithMsg("到账时间不能在过期时间之后！");
        }
        Date grantDateTime = new Date();
        if(dto.getAccountDateTime().getTime() < grantDateTime.getTime()) {
            return JsonResult.paramErrWithMsg("到账时间不能在分发时间之前！");
        }
        //如果积分有效期在到账时间之前
        dto.setGrantDateTime(grantDateTime);
        dto.setCreater(adminVo.getId());
        dto.setUpdater(adminVo.getId());
        dto.setEnterpriseId(adminVo.getEnterpriseId());
        return JsonResult.success(service.saveIntegralGrant(dto));
    }

}
